Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

388
Visualizações
[Service Worker]: How to retry post request if api call fail due to server down?

I want to retry the POST API call when the previous API request failed due to an API down or internet failure. I used setInterval to achieve that feature. But the issue I'm facing is, the setInternal timer stops if the user force stop the chrome completely.

In this case, I want to automatically start the retry whenever the browser opens again. How to achieve that. I tried using periodic sync, but that's working only if I install the app. Without installing the app, how to retry the API call? Is there any event available in service worker to listen for the browser open?

I tried to achieve it using the periodicSync event. But in order to use that feature, users need to install our web app. So is there any other feature available like sync that I can use without installing the app itself to achieve the desired behaviour?

self.addEventListener('sync', function (e) {
  if (e.tag === SYNC_USERS) return handleSyncUsers();
});

async function handleSyncUsers() {
  const syncResult = await syncUsers();
  const failedRequests = syncResult.filter(
    (res) => res.status === 'rejected'
  );
  
  if (failedRequests.length) {        
    return startPeriodicSync();
  } else {
    return clearPeriodicSync();
  }
}    

const startPeriodicSync = () => {
  periodicSyncTimer = setInterval(handleSyncUsers, 5000);
};

const clearPeriodicSync = () => {
  clearInterval(periodicSyncTimer);
  periodicSyncTimer = null;
};
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda